package com.mystore.author.security;

import com.mystore.author.security.filter.SecurityFilter;
import com.mystore.author.security.manager.SecurityDecisionManager;
import com.mystore.author.security.voter.CustomAccessDecisionVoter;
import com.mystore.author.security.web.filter.ApplicationWebFilter;
import com.mystore.common.utils.security.Md5Utils;
import com.mystore.framework.service.impl.CustomUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.vote.AffirmativeBased;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableWebSecurity
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    private SecurityDecisionManager securityDecisionManager;
    @Resource
    private SecurityFilter securityFilter;
    @Resource
    private CustomUserDetailsService customUserDetailsService;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        String[] userRequests=new String[]{"/console/user/**"};
        http.csrf().disable().authorizeRequests()
                .antMatchers(userRequests).hasRole("user")
                .antMatchers("/manage/**").hasRole("admin")
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
                        o.setSecurityMetadataSource(securityFilter);
                        o.setAccessDecisionManager(securityDecisionManager);
                        return o;
                    }
                })
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/author/user/loginPage.do")
                .and()
                .logout();
        http.addFilterBefore(applicationWebFilter(), UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());
    }
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                System.out.println("md5:"+Md5Utils.getMd5(charSequence.toString()));
                return Md5Utils.getMd5(charSequence.toString());
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                System.out.println("密码："+s);
                System.out.println(Md5Utils.getMd5(charSequence.toString()));
                return s.equals(Md5Utils.getMd5(charSequence.toString()));
            }
        };
    }
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    @Bean
    public ApplicationWebFilter applicationWebFilter(){
        return new ApplicationWebFilter();
    }
}